//
//  HNHappyFamilyAboutMeController.m
//  XDEducation
//
//  Created by 星道 on 17/6/9.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNHappyFamilyAboutMeController.h"
#import "HNHappyFaimlyAboutMeCell.h"
#import "HNHappyFamilyAboutMeModel.h"


@interface HNHappyFamilyAboutMeController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *DataArr;
@property (strong, nonatomic) UIButton *TempButton;
@property (nonatomic, assign) NSInteger SelectIndex;
@property (nonatomic, assign) NSInteger Index;
@end

@implementation HNHappyFamilyAboutMeController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.tableHeaderView = [UIView new];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100
    ;

    [self.tableView registerNib:[UINib nibWithNibName:[HNHappyFaimlyAboutMeCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];

    //0今日 1记录
    self.Index = 1;
    self.SelectIndex = 0;
    self.TempButton = (UIButton *)[self.view viewWithTag:1100];
    self.TempButton.selected = YES;

    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

        [self RefreshData];
    }];

    [self.tableView.mj_footer beginRefreshing];



    switch (self.HNHappyFamilyAboutMeType)
    {
        case HNHappyFamilyAboutMeActionNotes:self.title = @"行为记录";break;
        case HNHappyFamilyAboutMeGrowUp:self.title = @"成长建议";break;
        case HNHappyFamilyAboutMeSuggest:self.title = @"行为建议";break;
        default:break;
    }
}







#pragma mark - 网络请求

/**
 获取数据
 */
- (void)RefreshData
{
    NSDictionary *Params = @{
                             @"uid"     :[AccountTool shareAccountTool].currentAccount.uid,
                             @"action"  :[self GetAction],
                             @"page"    :@(self.Index)
                             };
    [XDHttpTool get:BaseURL(@"user.do?") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Params success:^(id responseObj) {

        [self EndRefresh];
        if ([responseObj isKindOfClass:[NSArray class]])
        {
            if ([responseObj count] == 0)
            {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else
            {
                [responseObj enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [self.DataArr addObject:[HNHappyFamilyAboutMeModel mj_objectWithKeyValues:obj]];
                }];
            }
            self.Index += 1;
            [self.tableView reloadData];
        }
        else
        {
            [MBProgressHUD showError:responseObj[@"msg"]];
        }
    } failure:^(NSError *error) {
        [self EndRefresh];
        [MBProgressHUD showError:XDNetWorkError];
    }];

}







#pragma mark - Fuction
/**
 结束刷新
 */
- (void)EndRefresh
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
    });
}




/**
 根据Type值及按钮值得到网络请求的Action

 @return Action=NSString
 */
- (NSString *)GetAction
{
    //行为记录
    if (self.HNHappyFamilyAboutMeType == HNHappyFamilyAboutMeActionNotes)
    {
        return self.SelectIndex == 0?@"getActionHistoryByYesToday":@"getActionHistory";
    }
    //成长建议
    else if (self.HNHappyFamilyAboutMeType == HNHappyFamilyAboutMeGrowUp)
    {
        return self.SelectIndex == 0?@"getGrowthSuggestByYestoday":@"getGrowthSuggest";
    }
    //行为建议
    else if (self.HNHappyFamilyAboutMeType == HNHappyFamilyAboutMeSuggest)
    {
        return self.SelectIndex == 0?@"getActionSuggestByYestoday":@"getActionSuggest";
    }
    else
    {
        return @"";
    }
}



/**
 下拉隐藏导航栏 上推显示导航栏

 @param scrollView tableViewScrollView
 @param velocity 方向
 @param targetContentOffset 坐标
 */
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if(velocity.y>0)
    {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    else
    {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}





#pragma mark - Action

/**
 切换 今日 明日

 @param sender  Button
 */
- (IBAction)ClickSelectTodayOrTomorrow:(UIButton *)sender
{
    if (self.TempButton == sender) return;

    self.TempButton.selected = !self.TempButton.selected;
    sender.selected = !sender.selected;
    self.TempButton = sender;

    //1100今日 1101记录
    self.SelectIndex = sender.tag - 1100;

    self.Index = 1;
    [self.DataArr removeAllObjects];
    [self.tableView reloadData];
    [self.tableView.mj_footer beginRefreshing];
}








#pragma mark - TableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.DataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNHappyFaimlyAboutMeCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.Model = self.DataArr[indexPath.row];
    return Cell;
}




#pragma mark - Setter
- (NSMutableArray *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}

@end
